package levelSelect;

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import networking.ClientConnect;
import networking.SMap;

import main.Main;


@SuppressWarnings("serial")
public class LevelSelect extends JPanel implements KeyListener
{
	private int maps = 1;
	private ArrayList<Icon> icons = new ArrayList<Icon>();
	private int selectedX = 0;
	private int selectedY = 0;
	private Main main;
	private boolean custom = false;
	private boolean connected = false;

	public LevelSelect(Main m, boolean b) 
	{
		main = m;
		custom = b;
		setSize(600,600);
		if (!custom) 
		{
			/*File inputFile = new File("maps/Map" + maps + ".snake");
			while (inputFile.exists()) 
			{
				inputFile = new File("maps/Map" + maps + ".snake");
				maps++;
			}
			for (int i = 1; i < maps - 1; i++) 
			{
				icons.add(new Icon("maps/Map" + i + ".snake"));
			}*/
			try 
			{
				URL inputURL = new URL("http://zmtn.dyndns-web.com/maps/Map" + maps + ".snake");
				while (inputURL.getContent() != null) 
				{
					inputURL = new URL("http://zmtn.dyndns-web.com/maps/Map" + maps + ".snake");
					if(inputURL.getContent() != null) 
					{
						icons.add(new Icon(new URL("http://zmtn.dyndns-web.com/maps/Map" + maps + ".snake")));
					}
					maps++;
					System.out.println("http://zmtn.dyndns-web.com/maps/Map" + maps + ".snake");
				}
			} 
			catch (MalformedURLException e) 
			{
				//e.printStackTrace();
			} 
			catch (IOException e) 
			{
				//e.printStackTrace();
			}
		}
		else
		{
			/*maps = 0;
			File folder = new File("custom/");
			File[] fileArray = folder.listFiles();
			maps = fileArray.length;
			System.out.println(maps);
			for (int i = 0; i < maps; i++) 
			{
				String file = fileArray[i].getAbsolutePath();
				System.out.println(file);
				icons.add(new Icon(file));
			}*/
			connected = ClientConnect.Connect();
			if(connected)
			{
				ArrayList<SMap> list = ClientConnect.getCustomMaps();
				for(int i=0; i<list.size(); i++)
				{
					icons.add(new Icon(list.get(i)));
				}
				//ClientConnect.Disconnect();
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Error: Could not connect to server");
				main.backToMainMenu();
			}
			
			
		}
		if(connected || !custom)
		{
			setLayout(new GridLayout(maps/3, 3));
			for(int i=0; i<icons.size(); i++)
			{
				add(icons.get(i), i);
			}
			
			icons.get(0).setSelected(true);
			addKeyListener(this);
			setFocusable(true);
		}
	}
	
	public void paintComponent(Graphics g)
	{
		for(int i=0; i<icons.size(); i++)
		{
			icons.get(i).repaint();
		}
	}
	
	private void switchIcon() 
	{
		for(int i=0; i<icons.size(); i++)
		{
			if(i == (selectedX) + (selectedY*3))
			{
				icons.get(i).setSelected(true);
			}
			else
			{
				icons.get(i).setSelected(false);
			}
		}
		for(int i=0; i<icons.size(); i++)
		{
			icons.get(i).repaint();
		}
	}

	public void keyPressed(KeyEvent e) 
	{
	}



	public void keyReleased(KeyEvent e) 
	{
		if(e.getKeyCode() == KeyEvent.VK_UP)
		{
			if(selectedY != 0)
			{
				selectedY--;
				switchIcon();
			}
			else
			{
				selectedY = icons.size()/3;
				switchIcon();
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_DOWN)
		{
			if(selectedY != icons.size()/3)
			{
				selectedY++;
				switchIcon();
			}
			else
			{
				selectedY = 0;
				switchIcon();
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			if(selectedX != 0)
			{
				selectedX--;
				switchIcon();
			}
			else
			{
				selectedX = 2;
				switchIcon();
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			if(selectedX != 2)
			{
				selectedX++;
				switchIcon();
			}
			else
			{
				selectedX = 0;
				switchIcon();
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			int t = (selectedX) + (selectedY*3) + 1;
			hitButton(t);
		}
		else if(e.getKeyCode() == KeyEvent.VK_M)
		{
			removeKeyListener(this);
			setFocusable(false);
			main.backToMainMenu();
		}
	}
	
	private void hitButton(int i)
	{
		//main.mainMenuChooseLevel(i);
		for(int j=0; j<icons.size(); j++)
		{
			if(icons.get(j).isSelected())
			{
				main.mainMenuPickLevel(icons.get(j).getMap());
			}
		}
		
		removeKeyListener(this);
		setFocusable(false);
	}



	public void keyTyped(KeyEvent e) 
	{
		
	}

	

}
